package com.rrd.hive.util;

public class StringToAscii {

    private static String toHexUtil(int n) {
        String rt = "";
        switch (n) {
            case 10:
                rt += "A";
                break;
            case 11:
                rt += "B";
                break;
            case 12:
                rt += "C";
                break;
            case 13:
                rt += "D";
                break;
            case 14:
                rt += "E";
                break;
            case 15:
                rt += "F";
                break;
            default:
                rt += n;
        }
        return rt;
    }

    public static String toHex(int n) {
        StringBuilder sb = new StringBuilder();
        if (n / 16 == 0) {
            return StringToAscii.toHexUtil(n);
        } else {
            String t = StringToAscii.toHex(n / 16);
            int nn = n % 16;
            sb.append(t).append(StringToAscii.toHexUtil(nn));
        }
        return sb.toString();
    }

    public static String parseAscii(String str) {
        StringBuilder sb = new StringBuilder();
        byte[] bs = str.getBytes();
        for (byte element : bs) {
            sb.append(StringToAscii.toHex(element));
        }
        return sb.toString();
    }

    public static void main(String args[]) {
        String s = "盖";
        System.out.println(StringToAscii.parseAscii(s));
    }
}
